package com.agent.action.service.Impl;

import com.agent.action.entity.DifyWorkFlowEntity;
import com.agent.action.entity.GenerateMarkDownEntity;
import com.agent.action.entity.QuestionEntity;
import com.agent.action.entity.TestEntity;
import com.agent.action.interfaces.WorkFlowClient;
import com.agent.action.param.*;
import com.agent.action.service.GenerateQuestionService;
import com.agent.action.vo.QuestionVO;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import feign.Logger;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;

@Slf4j
@Service
public class GenerateQuestionServiceImpl implements GenerateQuestionService {
    @Resource
    private WorkFlowClient<QuestionEntity> workFlowClient;
    @Resource
    private WorkFlowClient<TestEntity> testWorkFlowClient;
    @Resource
    private WorkFlowClient<GenerateMarkDownEntity> markDownWorkFlowClient;

    /**
     * 流式传输数据块(生成单个题目)
     * @param questionParam
     * @return
     */
    @Override
    public Flux<QuestionVO> streamQuestion(QuestionParam questionParam) {
        QuestionEntity questionEntity=new QuestionEntity();
        DifyWorkFlowEntity<QuestionEntity> difyWorkFlowEntity=new DifyWorkFlowEntity<>();
        questionEntity.setContent(questionParam.getContent());
        questionEntity.setType(questionParam.getType());
        difyWorkFlowEntity.setUser(questionParam.getUserId());
        difyWorkFlowEntity.setInputs(questionEntity);
        return ActionFlux(workFlowClient.stream(difyWorkFlowEntity));
    }

    /**
     * 流式传输数据块(生成练习)
     * @param testParam
     * @return
     */
    @Override
    public Flux<QuestionVO> streamTest(TestParam testParam) {
        TestEntity testEntity=testParamToTestEntity(testParam);
        DifyWorkFlowEntity<TestEntity> difyWorkFlowEntity=new DifyWorkFlowEntity<>();
        difyWorkFlowEntity.setInputs(testEntity);
        difyWorkFlowEntity.setUser(testParam.getUserId());
        return ActionFlux(testWorkFlowClient.stream(difyWorkFlowEntity));
    }

    /**
     * 暂停
     * @param pauseParam
     */
    @Override
    public void pause(FlowPauseParam pauseParam) {
        if(pauseParam.getEntity().equals("test")){
            testWorkFlowClient.pause(pauseParam);
        }else if(pauseParam.getEntity().equals("question")) {
            workFlowClient.pause(pauseParam);
        }else if(pauseParam.getEntity().equals("markdown")){
            markDownWorkFlowClient.pause(pauseParam);
        }
    }

    /**
     * 流式传输数据块(生成MarkDown)
     * @param markDownParam
     * @return
     */
    @Override
    public Flux<QuestionVO> streamMarkDown(MarkDownParam markDownParam) {
        GenerateMarkDownEntity generateMarkDownEntity=new GenerateMarkDownEntity();
        DifyWorkFlowEntity<GenerateMarkDownEntity> difyWorkFlowEntity=new DifyWorkFlowEntity<>();
        generateMarkDownEntity.setContent(markDownParam.getContent());
        generateMarkDownEntity.setType(markDownParam.getType());
        //TODO 获取到相关的检索数据(RAG知识库检索)
        difyWorkFlowEntity.setInputs(generateMarkDownEntity);
        difyWorkFlowEntity.setUser(markDownParam.getUserId());
        return ActionFlux(markDownWorkFlowClient.stream(difyWorkFlowEntity));
    }

    /**
     * TestParam->TestEntity
     */
    private TestEntity testParamToTestEntity(TestParam testParam){
        TestEntity testEntity=new TestEntity();
        testEntity.setContent(testParam.getContent().toString());
        testEntity.setTotal(testParam.getTotal());
        testEntity.setChoice(testParam.getChoice());
        testEntity.setFill(testParam.getFill());
        testEntity.setEssay(testParam.getEssay());
        return testEntity;
    }

    /**
     * 响应流处理Flux<String>->Flux<QuestionVO>
     * @param stream
     * @return
     */
    private Flux<QuestionVO> ActionFlux(Flux<String> stream){
        log.info("准备开始流式传输!");
        return stream.map(JSONObject::parseObject)
                .filter(jsonObject->jsonObject.get("event").equals("text_chunk"))//过滤数据块
                .map(jsonObject -> {
                    String task_id=jsonObject.get("task_id").toString();//获取任务id(task_id)
                    String text= JSON.parseObject(jsonObject.get("data").toString()).get("text").toString();//转换成字符串
                    System.out.print(text);
                    return new QuestionVO(task_id,text);
                })
                .doFinally(signalType ->{
                    log.info("流式传输结束!");
                });
    }
}
